package com.example.day11;

import android.app.Service;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView auto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        auto=findViewById(R.id.auto);
        String [] arr={"aa","aab","aac"};
        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arr);
        auto.setAdapter(arrayAdapter);
        String textname = "jsontest.txt";
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(textname));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            StringBuffer result = new StringBuffer();
            while (true) {
                if (!((line = bufReader.readLine()) != null)) {
                    break;
                }
                result.append(line);

            }
            jiexi(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void jiexi(String line){
        try {
            Log.e("######11",line);
            JSONArray jsA = new JSONArray(line);
            for(int i=0;i<jsA.length();i++){
                Log.e("######"+i,jsA.getString(i));
                org.json.JSONObject jso = jsA.getJSONObject(i);
                String comment_updated_at = jso.getString("comment_updated_at");
                String commentable = jso.getString("commentable");
                String comments_count = jso.getString("comments_count");
                String desc = jso.getString("desc");
                String first_shared_at = jso.getString("first_shared_at");
                String free_in_paid_book = jso.getString("free_in_paid_book");
                String has_audio = jso.getString("has_audio");
                String has_video = jso.getString("has_video");
                String id = jso.getString("id");
                String is_liked = jso.getString("is_liked");
                String last_compiled_at = jso.getString("last_compiled_at");
                String likes_count = jso.getString("likes_count");
                String list_image = jso.getString("list_image");

                String paid = jso.getString("paid");
                String slug = jso.getString("slug");
                String title = jso.getString("title");
                String total_rewards_count = jso.getString("total_rewards_count");
                String views_count = jso.getString("views_count");

                    org.json.JSONObject jsonotebook = jso.getJSONObject("notebook");

                String id1 = jsonotebook.getString("id");




                String user = jsonotebook.getString("user");
                org.json.JSONObject jsonuser = new org.json.JSONObject(user);

                String id4 = jsonuser.getString("id");
                if( user.contains("member")){
                    String member = jsonuser.getString("member");
                    org.json.JSONObject jsonmember = new org.json.JSONObject(member);
                    String expires_at = jsonmember.getString("expires_at");
                    String type = jsonmember.getString("type");
                }

                String nickname = jsonuser.getString("nickname");
                String sns_nicknames = jsonuser.getString("sns_nicknames");


                String avatar = jsonuser.getString("avatar");
                String badges = jsonuser.getString("badges");
                if(badges.length()>2){
                    JSONArray jsAbadges = new JSONArray(badges);
                    for(int j=0;j<jsAbadges.length();j++){
                        org.json.JSONObject jso2 = jsAbadges.getJSONObject(j);
                        String icon = jso2.getString("icon");
                        String id3 = jso2.getString("id");
                        if(jso2.toString().contains("image_url")){
                            String image_url = jso2.getString("image_url");
                        }
                        String intro_url = jso2.getString("intro_url");
                        String text = jso2.getString("text");
                    }
                }
      }
        } catch (JSONException e) {
            Log.e("####报错",e.toString());
            e.printStackTrace();
        }
    }

    /**
     * String转化为实体类Book  Gson
     */
    public void strToObjectG(String str){
        Gson gson = new Gson();
        TextJavaBeen beentest=gson.fromJson(str,TextJavaBeen.class);
        Log.e("###",beentest.getItem().get(0).getTitle());
    }


    /**
     * map转json Gson
     */
    public void mapTojson(){
        Gson gson = new Gson();

        List<String> list = Arrays.asList("1", "a", "3", "rt", "5");
        Log.e("######","---->list convert jsonStr:" + gson.toJson(list));
        Map<String, Object> content = new HashMap<String, Object>();
        content.put("name", "xuanyouwu");
        content.put("age", "26");
        Log.e("","---->map convert jsonStr:" + gson.toJson(content));

    }


    /**
     * String转化为实体类Book  fastJSON
     */

    public void StrToObject(String str) {
        TextJavaBeen beentest=JSON.parseObject(str,TextJavaBeen.class);
        Log.e("###",beentest.getItem().get(0).getTitle());
    }

    /**
     * String转化为jsonObject fastJson
     */
    public void StrToJsonObject(String str) {
        JSONObject jsonObj = JSON.parseObject(str.toString());
        for(Map.Entry<String, Object> entry : jsonObj.entrySet()){
            Log.e("###",entry.getKey()+"--"+entry.getValue());
        }
    }

    /**
     * map和json互相转换
     */
    public void mapTo(){
        Map<String,String> map = new HashMap<>();
        map.put("name", "教父三部曲");
        map.put("author", "马里奥·普佐");
        map.put("price", "100");
        //map转换为json
        String json = JSON.toJSONString(map);
        //json转换为map
        Map map1 = JSON.parseObject(json);
        Map<String,String> map2 = (Map<String,String>)JSON.parse(json);

    }
}
